package com.sky.controller.admin;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.BaseException;
import com.sky.exception.PasswordErrorException;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController("adminEmployeeController")
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")

    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();

        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 添加员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加接口方法")
    public Result<Employee> addEmployee(@RequestBody EmployeeDTO employeeDTO){
        System.out.println(employeeService.toString());
        //他先走令牌方法
        // 添加员工的前端接口在哪
        try {
            employeeService.addEmployee(employeeDTO);
        } catch (Exception e) {
            throw new BaseException();
        }
        return Result.success();
    }

    /**
     * 分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageSelectEmployee(EmployeePageQueryDTO employeePageQueryDTO){
        System.out.println(employeePageQueryDTO);
        PageResult result = null;
        try {
            result = employeeService.pageSelectEmployee(employeePageQueryDTO);
        } catch (BaseException e) {
            return Result.error(MessageConstant.ORDER_NOT_EMP);
        }
        return Result.success(result);
    }

    /**
     * 状态启用禁用接口
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("状态接口")
    @PostMapping("/status/{status}")
    public Result<Integer> updateEnableDisable(@PathVariable Integer status, Integer id){
        System.out.println(status);
        System.out.println(id);
        try {
            employeeService.updateEnableDisable(status,id);
        } catch (Exception e) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success();
    }

    /**
     * 根据id查询员工数据进行回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> selectByIdEmployee(@PathVariable Integer id){
        Employee employee = null;
        try {
            employee = employeeService.selectByIdEmployee(id);
        } catch (Exception e) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success(employee);
    }


    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    public Result<Employee> updateEmployee(@RequestBody EmployeeDTO employeeDTO){
        try {
            employeeService.updateEmployee(employeeDTO);
        } catch (Exception e) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success();
    }


    /**
     * 修改员工密码
     * @param passwordEditDTO
     * @return
     */
    @PutMapping("/editPassword")
    public Result updateEmployeePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        try {
            employeeService.updateEmployeePassword(passwordEditDTO);
        } catch (PasswordErrorException e) {
            return Result.error(MessageConstant.PASSWORD_EDIT_FAILED);
        }
        return Result.success();
    }
}
